//
//  LZClient.m
//  tuangou
//
//  Created by quan jimmy on 11-11-26.
//  Copyright 2011年 tencent. All rights reserved.
//

#import "LZClient.h"
#import "SBJsonParser.h"


@implementation LZClient
@synthesize appDelegate;
- (id)init {
    self = [super init];
    if (self) {
        appDelegate = (tuangouAppDelegate *)[UIApplication sharedApplication].delegate;
    }
    
    return self;
}

- (ASIHTTPRequest *)requestCategory:(NSInteger)tag delegate:(id)requestDelegate{
    NSString *urlstr = [NSString stringWithFormat: @"%@/api/category", LZ_TUAN_URL];
    NSURL *url = [NSURL URLWithString: urlstr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    //[self initOAuthRequestHeader:request];
    [request setDelegate:requestDelegate];
    [request setTag:tag];
    [request startAsynchronous];
    return request;
}

- (ASIHTTPRequest *)requestDeal:(NSInteger)cateId page:(NSInteger)page limit:(NSInteger)limit tag:(NSInteger)tag delegate:(id)requestDelegate {
    
    NSString *urlstr = [NSString stringWithFormat: @"%@/api/deal?cateId=%d&page=%d&limit=%d", LZ_TUAN_URL,cateId, page, limit];
    NSURL *url = [NSURL URLWithString: urlstr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    //[self initOAuthRequestHeader:request];
    [request setDelegate:requestDelegate];
    [request setTag:tag];
    [request startAsynchronous];
    return request;
}

- (NSInteger)requestDealTotal:(NSInteger *)cateId  city:(NSString *)city {
    NSString *urlstr = [NSString stringWithFormat: @"%@api/dealtotal?cateId=%d&cityName=%@", LZ_TUAN_URL,cateId,city];
    NSURL *url = [NSURL URLWithString: urlstr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    //[self initOAuthRequestHeader:request];
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        NSString *response = [request responseString];
        if (request.responseStatusCode == 200) {
            SBJsonParser *parser = [[SBJsonParser alloc] init];
            NSArray *rows = [parser objectWithString:response];
            if (!rows) {
                [parser release];
                return 0;
            }
            for (NSDictionary *row in rows) {
                if (![row isKindOfClass:[NSString class]]) {
                    return [[row objectForKey:@"dealtotal"] intValue];
                }
            }
            [parser release];
        } else {
            [LZClient alertWithMessage:response];
            return 0;
        }
        
     } else {
        NSLog(@"request deal total error: %@", error);
        return 0;
    }
    return 0;
}

+ (void)alertWithMessage:(NSString *)message
{
    /* open an alert with an OK button */
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"乐折吧" 
                                                    message:message
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles: nil];
    [alert show];
    [alert release];
}

@end
